/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaConvinada.java
 *
 * Created on 03-may-2011, 11:58:24
 */
package es.fpsampayo.fin2006.consultas;

import es.fpsampayo.fin2006.PadronForm;
import es.fpsampayo.fin2006.gui.forms.CatForm;
import es.fpsampayo.recursos.OperacionesDB;
import javax.swing.JOptionPane;

/**
 *
 * @author fran
 */
public class ConsultaMultiple extends javax.swing.JDialog {

    /** Creates new form ConsultaConvinada */
    public ConsultaMultiple(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cargaCamposConcellos();
        comboConcello.setSelectedIndex(-1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        comboConcello = new javax.swing.JComboBox();
        labelConcello = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        labelBaseDatos = new javax.swing.JLabel();
        comboBaseDatos = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        labelCampo = new javax.swing.JLabel();
        comboCampo = new javax.swing.JComboBox();
        labelValor = new javax.swing.JLabel();
        campoValor = new javax.swing.JTextField();
        botonAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        labelConcello.setText("Concello:");

        labelBaseDatos.setText("Base Datos:");

        comboBaseDatos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Padron", "Cat" }));
        comboBaseDatos.setSelectedIndex(-1);
        comboBaseDatos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBaseDatosActionPerformed(evt);
            }
        });

        labelCampo.setText("Campo:");

        labelValor.setText("Valor:");

        campoValor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoValorActionPerformed(evt);
            }
        });

        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelConcello)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                        .addComponent(comboConcello, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelBaseDatos)
                        .addGap(18, 18, 18)
                        .addComponent(comboBaseDatos, 0, 167, Short.MAX_VALUE))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelCampo)
                            .addComponent(labelValor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campoValor)
                            .addComponent(comboCampo, 0, 167, Short.MAX_VALUE)
                            .addComponent(botonAceptar))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelConcello)
                    .addComponent(comboConcello, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelBaseDatos)
                    .addComponent(comboBaseDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCampo)
                    .addComponent(comboCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelValor)
                    .addComponent(campoValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(botonAceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoValorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoValorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoValorActionPerformed

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        // TODO add your handling code here:
        if("Cat".equals(comboBaseDatos.getSelectedItem().toString())){
            if ((OperacionesDB.getIdCat(comboCampo.getSelectedItem().toString(), campoValor.getText())) != 0){
                CatForm cat = new CatForm(null, false, OperacionesDB.getIdCat(comboCampo.getSelectedItem().toString(), campoValor.getText()));
                cat.setLocationRelativeTo(this);
                cat.setVisible(true);
                this.dispose();
            }else{
                JOptionPane.showMessageDialog(this, "No existe ningún valor coincidente.");
            }
        }else if ("Padron".equals(comboBaseDatos.getSelectedItem().toString())){
            if ((OperacionesDB.getIdPadron(comboCampo.getSelectedItem().toString(), campoValor.getText())) != 0){
                PadronForm padron = new PadronForm(null, false, OperacionesDB.getIdPadron(comboCampo.getSelectedItem().toString(), campoValor.getText()));
                padron.setLocationRelativeTo(this);
                padron.setVisible(true);
                this.dispose();
            }else{
                JOptionPane.showMessageDialog(this, "No existe ningún valor coincidente.");
            }
        }
    }//GEN-LAST:event_botonAceptarActionPerformed

    private void comboBaseDatosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBaseDatosActionPerformed
        // TODO add your handling code here:
        cargaCamposValores(comboBaseDatos.getSelectedItem().toString());
    }//GEN-LAST:event_comboBaseDatosActionPerformed

    private void cargaCamposConcellos (){
        
        comboConcello.setModel(OperacionesDB.consultaConcelloComboBox());
    }
    
    private void cargaCamposValores (String BaseDatos){
        comboCampo.setModel(OperacionesDB.consultaCampoComboBox(BaseDatos));
        comboCampo.setSelectedItem(null);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ConsultaMultiple dialog = new ConsultaMultiple(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JTextField campoValor;
    private javax.swing.JComboBox comboBaseDatos;
    private javax.swing.JComboBox comboCampo;
    private javax.swing.JComboBox comboConcello;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel labelBaseDatos;
    private javax.swing.JLabel labelCampo;
    private javax.swing.JLabel labelConcello;
    private javax.swing.JLabel labelValor;
    // End of variables declaration//GEN-END:variables
}
